package com.atguigu.order.listener;

import com.atguigu.common.to.SeckillOrderTo;
import com.atguigu.order.entity.OrderEntity;
import com.atguigu.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * description: OrderSeckillListener
 * date: 2023/1/8 23:58
 * author: MR.孙
 */
@Slf4j
@RabbitListener(queues = "order.seckill.order.queue")
@Component
public class OrderSeckillListener {


    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void listener(SeckillOrderTo seckillOrder, Channel channel, Message message) throws IOException {
        log.info("收到过期的订单消息，准备关闭订单" + seckillOrder.getOrderSn());
        try {
            orderService.createSeckillOrder(seckillOrder);
            //手动ACK
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            //执行失败重新发送到消息队列, 方便其他人消费
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }


}
